﻿using System;
using System.Collections.Generic;
using System.Text;
using uhul.data;
using System.Data.SqlTypes;


namespace testApp
{
  public class GeometryStorageTest
  {



    public static void testMe()
    {
      double ord;
      SqlBytes gdo;
      SqlBytes wkb;
      string wkt;
      byte[] b;


      //wkt = sqlGeometryUtils.GdoBlob2Wkt(sqlGeometryUtils.Wkb2GdoBlob(sqlGeometryUtils.GdoBlob2Wkb(sqlGeometryUtils.Wkt2GdoBlob("MULTIPOLYGON(((3 3,3 10,10 10,10 3,3 3),(3 3,7 3,7 7,3 7, 3 3)),((15 15,15 25,25 25,25 15,15 15)))"))));

      string hexStr = "C6FFD20FBC8CCF11ABDE08003601B7690300000028030000C5FFD20FBC8CCF11ABDE08003601B769F4010000C3FFD20FBC8CCF11ABDE08003601B769140000000FD6FF19F17126C1C845B534993232C100000000000000005A677C1F2C7226C1BC57AD24853232C100000000000000004D2CF07D957226C14B3ACAF9723232C10000000000000000E0DBF437A97226C192205C494A3232C10000000000000000F88C4418C67226C1CC5EB6C5F53132C100000000000000001C093458A97226C1DE21C538FA3132C100000000000000005BD3BC93D87226C1B8E68E32C63132C10000000000000000959F545B007326C115014E9F483132C100000000000000003F6F2A620A7326C16DE5257F433132C10000000000000000F81BEDD0177326C1A417B50F113132C100000000000000009A7B48D0077326C1B87878AB073132C100000000000000002E3BC46F027326C141B80266123132C10000000000000000EA5E2765ED7226C18CDCD3613C3132C100000000000000008927BB89B97226C1F99D2603C13132C10000000000000000D8F4A0F0B07126C1F5A0A038E13132C1000000000000000061FE0A39DE7126C1E1455F314E3232C100000000000000001630810BE67126C128999C0E543232C1000000000000000043740824E97126C1C1E4461D5F3232C100000000000000004F081DE4007226C1CEC29E725F3232C100000000000000000FD6FF19F17126C1C845B534993232C100000000000000001C010000C6FFD20FBC8CCF11ABDE08003601B7690100000004010000C3FFD20FBC8CCF11ABDE08003601B7690A000000B58CD4538B7226C1BF7FF382063232C10000000000000000D6373011927226C1FB777D9A0A3232C10000000000000000C8D11CD97D7226C160234944203232C10000000000000000E83235C9317226C1FCE3BD3E343232C10000000000000000A27E177E1F7226C18E5A618A393232C1000000000000000089997D4E247226C1C05DF6AB2F3232C1000000000000000031B77B69487226C1812381BA273232C10000000000000000B79BE04B657226C15E8253571E3232C100000000000000002159C024837226C1A702EE7D0D3232C10000000000000000B58CD4538B7226C1BF7FF382063232C1000000000000000030070000C5FFD20FBC8CCF11ABDE08003601B7698C060000C3FFD20FBC8CCF11ABDE08003601B76945000000FDC1C033FB7026C1074147EFB23232C10000000000000000629F00FA267126C17AC5532FB13232C10000000000000000B72572F1447126C1A27DACFCAF3232C1000000000000000033C2DB7B407126C1077DE969A53232C1000000000000000005BF0D193E7126C1895FB1DE9B3232C1000000000000000061C5A9DE427126C1394547B6943232C1000000000000000091B586524E7126C1BF61A2CD923232C10000000000000000F295403A657126C1E48409FF933232C10000000000000000AC8F87D6777126C1F9A3A8BB9E3232C100000000000000001827BEA2817126C1E3C7988FAD3232C100000000000000006D8E735B8C7126C1F085C9D4B63232C100000000000000008F1A13DAAB7126C15C0531C8CC3232C10000000000000000ECA694FFBD7126C1C7A013DED53232C10000000000000000EFACDD0EC67126C1959C135BBF3232C10000000000000000DFA63FA3D87126C1956071ECAC3232C10000000000000000A9BEF3031F7226C1BB44F5B2973232C1000000000000000092E86524A77226C1DBDFD94A7B3232C100000000000000001D210351A87226C11EA4A7F8763232C100000000000000006745D4FC147226C1963FDF62953232C1000000000000000007B64AB8E27126C11EFCC475A43232C1000000000000000059BE2E4BBF7126C1221B482FA33232C100000000000000004D840D47C17126C162484EA2A03232C100000000000000005CC98E15D97126C1E7FBA9859E3232C1000000000000000032738143DE7126C1AB251D957B3232C1000000000000000043740824E97126C1C1E4461D5F3232C100000000000000001630810BE67126C128999C0E543232C100000000000000006D9049AED47126C1B05758EC603232C10000000000000000BCB37633C57126C11844A4EE763232C10000000000000000BEA25B3FBA7126C1AF5E455E7A3232C100000000000000005187159EB07126C1F146E695753232C10000000000000000834C3252C37126C16405BF35633232C100000000000000004A41B7E7F97026C106836B220B3232C1000000000000000055319576DF7026C18F1CE99C1A3232C1000000000000000098C11829D97026C1359BC731193232C100000000000000005798BE3F087126C1BDC799CAC13132C1000000000000000006BAF60D647126C1BCE7C0FEE53132C10000000000000000F58425E6767126C1B5183C44EA3132C10000000000000000D8F4A0F0B07126C1F5A0A038E13132C100000000000000005A0F5F2EAF7126C13FE59814DE3132C10000000000000000C11DA87B9C7226C1B29FC56AC13132C10000000000000000E275FD52557226C14A26A7C6B23132C10000000000000000E3FDB885407226C1F3052DE4A73132C10000000000000000C5909C3C2B7226C1F9D9C819A93132C10000000000000000037B4C342C7226C1FEBAD325AF3132C10000000000000000E1F08278067226C1E59997DFAF3132C10000000000000000952710F6E87126C1EF39B07CAC3132C100000000000000003C2EAA45CB7126C11F4773F4903132C10000000000000000BB48A134BB7126C1098849548D3132C10000000000000000B6F3FDC4837126C17FFACF66743132C10000000000000000B91803734A7126C1BC2363657B3132C10000000000000000797764B4227126C17347FF37B23132C1000000000000000036902E06F67026C1F20703C3B03132C100000000000000006C04E215D27026C11AA6B65CAD3132C10000000000000000A4721325BF7026C1414640FDB93132C10000000000000000A0F99C3BAB7026C1D061BEFCCA3132C100000000000000003F710045997026C182E673DEDE3132C10000000000000000723447BE9B7026C19A232B23E43132C10000000000000000EF8FF7FA937026C1E31A9FB50A3232C10000000000000000D82B2C48807026C1C0401078283232C1000000000000000005FBAF3B707026C159A65F22473232C100000000000000001F4C8AAF6C7026C16B61168E4A3232C1000000000000000058E4D78FE57026C19FE40E6F493232C100000000000000001E6CB14BEC7026C1B1F84D7D553232C10000000000000000B37E3369FF7026C18D25ACCD643232C10000000000000000D960E1E4547026C156B95091603232C10000000000000000DFDDCABA7B7026C11840F818693232C10000000000000000151E342BBD7026C109FB76AE7C3232C10000000000000000297AE033E77026C1B69F8C7D9B3232C10000000000000000FDC1C033FB7026C1074147EFB23232C100000000000000008C000000C6FFD20FBC8CCF11ABDE08003601B7690100000074000000C3FFD20FBC8CCF11ABDE08003601B76904000000BFB67EEAD37126C1CFBBB1B0AA3232C10000000000000000618DB349BB7126C1807D7412BF3232C10000000000000000618DB349BB7126C1FAB9A141AB3232C10000000000000000BFB67EEAD37126C1CFBBB1B0AA3232C100000000000000008C000000C3FFD20FBC8CCF11ABDE08003601B76905000000685BCD0AEE7126C120257679903132C100000000000000004DA25EA01F7226C1B306EF479C3132C1000000000000000082ACA7EE4A7226C14948A401953132C1000000000000000080D6FCF00B7226C103999DE97A3132C10000000000000000685BCD0AEE7126C120257679903132C10000000000000000";
      hexStr = "C6FFD20FBC8CCF11ABDE08003601B7690300000028030000C5FFD20FBC8CCF11ABDE08003601B769F4010000C3FFD20FBC8CCF11ABDE08003601B769140000000FD6FF19F17126C1C845B534993232C100000000000000005A677C1F2C7226C1BC57AD24853232C100000000000000004D2CF07D957226C14B3ACAF9723232C10000000000000000E0DBF437A97226C192205C494A3232C10000000000000000F88C4418C67226C1CC5EB6C5F53132C100000000000000001C093458A97226C1DE21C538FA3132C100000000000000005BD3BC93D87226C1B8E68E32C63132C10000000000000000959F545B007326C115014E9F483132C100000000000000003F6F2A620A7326C16DE5257F433132C10000000000000000F81BEDD0177326C1A417B50F113132C100000000000000009A7B48D0077326C1B87878AB073132C100000000000000002E3BC46F027326C141B80266123132C10000000000000000EA5E2765ED7226C18CDCD3613C3132C100000000000000008927BB89B97226C1F99D2603C13132C10000000000000000D8F4A0F0B07126C1F5A0A038E13132C1000000000000000061FE0A39DE7126C1E1455F314E3232C100000000000000001630810BE67126C128999C0E543232C1000000000000000043740824E97126C1C1E4461D5F3232C100000000000000004F081DE4007226C1CEC29E725F3232C100000000000000000FD6FF19F17126C1C845B534993232C100000000000000001C010000C6FFD20FBC8CCF11ABDE08003601B7690100000004010000C3FFD20FBC8CCF11ABDE08003601B7690A000000B58CD4538B7226C1BF7FF382063232C10000000000000000D6373011927226C1FB777D9A0A3232C10000000000000000C8D11CD97D7226C160234944203232C10000000000000000E83235C9317226C1FCE3BD3E343232C10000000000000000A27E177E1F7226C18E5A618A393232C1000000000000000089997D4E247226C1C05DF6AB2F3232C1000000000000000031B77B69487226C1812381BA273232C10000000000000000B79BE04B657226C15E8253571E3232C100000000000000002159C024837226C1A702EE7D0D3232C10000000000000000B58CD4538B7226C1BF7FF382063232C1000000000000000030070000C5FFD20FBC8CCF11ABDE08003601B7698C060000C3FFD20FBC8CCF11ABDE08003601B76945000000FDC1C033FB7026C1074147EFB23232C10000000000000000629F00FA267126C17AC5532FB13232C10000000000000000B72572F1447126C1A27DACFCAF3232C1000000000000000033C2DB7B407126C1077DE969A53232C1000000000000000005BF0D193E7126C1895FB1DE9B3232C1000000000000000061C5A9DE427126C1394547B6943232C1000000000000000091B586524E7126C1BF61A2CD923232C10000000000000000F295403A657126C1E48409FF933232C10000000000000000AC8F87D6777126C1F9A3A8BB9E3232C100000000000000001827BEA2817126C1E3C7988FAD3232C100000000000000006D8E735B8C7126C1F085C9D4B63232C100000000000000008F1A13DAAB7126C15C0531C8CC3232C10000000000000000ECA694FFBD7126C1C7A013DED53232C10000000000000000EFACDD0EC67126C1959C135BBF3232C10000000000000000DFA63FA3D87126C1956071ECAC3232C10000000000000000A9BEF3031F7226C1BB44F5B2973232C1000000000000000092E86524A77226C1DBDFD94A7B3232C100000000000000001D210351A87226C11EA4A7F8763232C100000000000000006745D4FC147226C1963FDF62953232C1000000000000000007B64AB8E27126C11EFCC475A43232C1000000000000000059BE2E4BBF7126C1221B482FA33232C100000000000000004D840D47C17126C162484EA2A03232C100000000000000005CC98E15D97126C1E7FBA9859E3232C1000000000000000032738143DE7126C1AB251D957B3232C1000000000000000043740824E97126C1C1E4461D5F3232C100000000000000001630810BE67126C128999C0E543232C100000000000000006D9049AED47126C1B05758EC603232C10000000000000000BCB37633C57126C11844A4EE763232C10000000000000000BEA25B3FBA7126C1AF5E455E7A3232C100000000000000005187159EB07126C1F146E695753232C10000000000000000834C3252C37126C16405BF35633232C100000000000000004A41B7E7F97026C106836B220B3232C1000000000000000055319576DF7026C18F1CE99C1A3232C1000000000000000098C11829D97026C1359BC731193232C100000000000000005798BE3F087126C1BDC799CAC13132C1000000000000000006BAF60D647126C1BCE7C0FEE53132C10000000000000000F58425E6767126C1B5183C44EA3132C10000000000000000D8F4A0F0B07126C1F5A0A038E13132C100000000000000005A0F5F2EAF7126C13FE59814DE3132C10000000000000000C11DA87B9C7226C1B29FC56AC13132C10000000000000000E275FD52557226C14A26A7C6B23132C10000000000000000E3FDB885407226C1F3052DE4A73132C10000000000000000C5909C3C2B7226C1F9D9C819A93132C10000000000000000037B4C342C7226C1FEBAD325AF3132C10000000000000000E1F08278067226C1E59997DFAF3132C10000000000000000952710F6E87126C1EF39B07CAC3132C100000000000000003C2EAA45CB7126C11F4773F4903132C10000000000000000BB48A134BB7126C1098849548D3132C10000000000000000B6F3FDC4837126C17FFACF66743132C10000000000000000B91803734A7126C1BC2363657B3132C10000000000000000797764B4227126C17347FF37B23132C1000000000000000036902E06F67026C1F20703C3B03132C100000000000000006C04E215D27026C11AA6B65CAD3132C10000000000000000A4721325BF7026C1414640FDB93132C10000000000000000A0F99C3BAB7026C1D061BEFCCA3132C100000000000000003F710045997026C182E673DEDE3132C10000000000000000723447BE9B7026C19A232B23E43132C10000000000000000EF8FF7FA937026C1E31A9FB50A3232C10000000000000000D82B2C48807026C1C0401078283232C1000000000000000005FBAF3B707026C159A65F22473232C100000000000000001F4C8AAF6C7026C16B61168E4A3232C1000000000000000058E4D78FE57026C19FE40E6F493232C100000000000000001E6CB14BEC7026C1B1F84D7D553232C10000000000000000B37E3369FF7026C18D25ACCD643232C10000000000000000D960E1E4547026C156B95091603232C10000000000000000DFDDCABA7B7026C11840F818693232C10000000000000000151E342BBD7026C109FB76AE7C3232C10000000000000000297AE033E77026C1B69F8C7D9B3232C10000000000000000FDC1C033FB7026C1074147EFB23232C100000000000000008C000000C6FFD20FBC8CCF11ABDE08003601B7690100000074000000C3FFD20FBC8CCF11ABDE08003601B76904000000BFB67EEAD37126C1CFBBB1B0AA3232C10000000000000000618DB349BB7126C1807D7412BF3232C10000000000000000618DB349BB7126C1FAB9A141AB3232C10000000000000000BFB67EEAD37126C1CFBBB1B0AA3232C100000000000000008C000000C3FFD20FBC8CCF11ABDE08003601B76905000000685BCD0AEE7126C120257679903132C100000000000000004DA25EA01F7226C1B306EF479C3132C1000000000000000082ACA7EE4A7226C14948A401953132C1000000000000000080D6FCF00B7226C103999DE97A3132C10000000000000000685BCD0AEE7126C120257679903132C10000000000000000";

      string hexStrWkb = "0106000000060000000103000000010000001D00000008AC1C5AD6C524C1CDCCCC0CA0372DC1EE7C3FF5F4C524C139B4C83625372DC1F853E325DDC524C16F1283401A372DC1DD240641D7C524C1D122DBF912372DC11B2FDDE4D5C524C19318045605372DC1CDCCCCCCD6C524C1986E1283F9362DC1E7FBA9B1DCC524C17B14AEC7EC362DC133333333EBC524C19CC420B0E8362DC1BA490C4203C624C13D0AD7A3EF362DC14260E5D015C624C196438BECBE362DC146B6F3FDC5C524C1BA490CC298362DC1666666A6AEC524C19EEFA706A8362DC13D0AD7E3ADC524C108AC1C5AA8362DC1448B6CA793C524C114AE47A1B3362DC179E9267185C524C1AE47E1BAB6362DC18B6CE7FB86C524C1AC1C5AA4B8362DC15839B40881C524C18B6CE7FBC1362DC10AD7A33083C524C1C520B072CB362DC1713D0A5782C524C191ED7C3FCD362DC1F4FDD4B897C524C14E621058DB362DC19A999999AFC524C193180456F0362DC14C378981B2C524C1FCA9F19203372DC1F853E3A5B7C524C18716D9CE23372DC1F853E3A5B7C524C14C3789412A372DC12DB29DEFAFC524C17B14AEC74B372DC1D9CEF713ACC524C1F2D24D6257372DC19CC420F09DC524C14E6210986E372DC1A01A2FDD8EC524C1CBA145B67D372DC108AC1C5AD6C524C1CDCCCC0CA0372DC10103000000010000000A000000EE7C3FF5F4C524C139B4C83625372DC1295C8F8200C624C108AC1CDAF6362DC1BA490C4203C624C13D0AD7A3EF362DC133333333EBC524C19CC420B0E8362DC1E7FBA9B1DCC524C17B14AEC7EC362DC1CDCCCCCCD6C524C1986E1283F9362DC11B2FDDE4D5C524C19318045605372DC1DD240641D7C524C1D122DBF912372DC1F853E325DDC524C16F1283401A372DC1EE7C3FF5F4C524C139B4C83625372DC1010300000001000000050000008B6CE7FB1CC624C1CDCCCC0C063A2DC18FC2F5E851C624C16DE7FBA9A9392DC121B0726809C624C1B29DEF67A4392DC1931804560FC624C13F355EFAC1392DC18B6CE7FB1CC624C1CDCCCC0C063A2DC10103000000010000000A0000008B6CE7FB1CC624C1CDCCCC0C063A2DC1931804560FC624C13F355EFAC1392DC1E7FBA9318FC524C175931844B7392DC1E7FBA9318FC524C137894120EA392DC16F1283007EC524C123DBF9FE033A2DC12731086C99C524C17D3F35DE063A2DC16ABC74D3D2C524C1D9CEF7130D3A2DC1000000C01CC624C1F0A7C60B093A2DC1C520B0F21CC624C1A01A2F1D063A2DC18B6CE7FB1CC624C1CDCCCC0C063A2DC101030000000100000004000000E7FBA9B154C624C1022B87560B3A2DC17368912D40C624C19EEFA746D8392DC14A0C02EB22C624C148E17A140A3A2DC1E7FBA9B154C624C1022B87560B3A2DC1010300000001000000110000000000004084C524C110583974E13A2DC13F355EFA78C524C1D9CEF793AE3A2DC1DF4F8D5779C524C1B4C876FE883A2DC1E5D0221B74C524C1DD2406817F3A2DC18716D9CE66C524C1EC51B89E693A2DC1759318445FC524C139B4C836613A2DC1D7A3707D60C524C1F2D24D625A3A2DC1A01A2F9D68C524C1759318C4553A2DC123DBF9BE50C524C133333333493A2DC15A643BDF3AC524C1B6F3FD546D3A2DC18195438B3AC524C1FCA9F112833A2DC1F6285C4F3AC524C1DF4F8D97923A2DC12B8716D951C524C1DBF97EAA063B2DC1AC1C5A245AC524C1E7FBA9F1F73A2DC1B072689164C524C17B14AE07ED3A2DC13D0AD7237CC524C1FED478A9E13A2DC10000004084C524C110583974E13A2DC1";

      b = HexStrToByteArray(hexStr);
      byte[] bWkb = HexStrToByteArray(hexStrWkb);
      gdo = sqlGeometryUtils.Wkb2GdoBlob(new SqlBytes(bWkb));


      string n = sqlGeometryUtils.GetGeomType(new SqlBytes(b));

      wkb = sqlGeometryUtils.GdoBlob2Wkb(new SqlBytes(b));
      gdo = sqlGeometryUtils.Wkb2GdoBlob(wkb);

      string hexStr1 = ByteArrayToString(gdo.Value);

      //porovnani geometrie pred a po konverzi
      for (int znPos = 0; znPos < hexStr.Length; znPos++)
        if (hexStr[znPos] != hexStr1[znPos])
          Console.WriteLine("chyba");

      gdo = sqlGeometryUtils.Wkt2GdoBlob("POINT(-32454320.345 -32453420.545)");
      wkt = sqlGeometryUtils.GdoBlob2Wkt(gdo);
      Console.WriteLine(wkt);
      wkb = sqlGeometryUtils.GdoBlob2Wkb(gdo);
      gdo = sqlGeometryUtils.Wkb2GdoBlob(wkb);
      
      gdo = sqlGeometryUtils.Wkt2GdoBlob("MULTIPOLYGON(((1 1,5 1,5 5,1 5,1 1),(2 2, 3 2, 3 3, 2 3,2 2)),((3 3,6 2,6 4,3 3)))");

      //gdo = sqlGeometryUtils.Wkt2GdoBlob("POINT(-32454320.345 -32453420.545)");
      ord = sqlGeometryUtils.GetWktEnvelopeCoord("POINT(-32454320.345 -32453420.545)", "xlo");
      gdo = sqlGeometryUtils.Wkt2GdoBlob("LINESTRING(20 20, 30 30, 40 40, 50 50)");
      wkt = sqlGeometryUtils.GdoBlob2Wkt(gdo);
      Console.WriteLine(wkt);
      gdo = sqlGeometryUtils.Wkt2GdoBlob("POLYGON((20 20,30 30,40 40,20 20),(25 25,26 26,30 30,25 25))");
      gdo = sqlGeometryUtils.Wkt2GdoBlob("POLYGON((10.2 30.5,200 400.1,120.7 99.3345,23 35.456,10.2 30.5),(15 15,15 20,20 20,15 15))");
      gdo = sqlGeometryUtils.Wkt2GdoBlob("MULTIPOLYGON(((1 1,5 1,5 5,1 5,1 1),(2 2, 3 2, 3 3, 2 3,2 2)),((3 3,6 2,6 4,3 3)))");
      //byte[] ada = sb.Value;
      wkt = sqlGeometryUtils.GdoBlob2Wkt(gdo);
      wkt = sqlGeometryUtils.GdoBlob2Wkt(gdo);
      Console.WriteLine(wkt);

      ord = sqlGeometryUtils.GetWktEnvelopeCoord("POLYGON((20 20, 30 30, 40 40, 20 20),(25 25, 26 26, 30 30, 25 25))", "xhi");

      wkb = sqlGeometryUtils.GdoBlob2Wkb(gdo);
      gdo = sqlGeometryUtils.Wkb2GdoBlob(wkb);
      wkt = sqlGeometryUtils.GdoBlob2Wkt(gdo);

      Console.Write("geom storage test done. Press Enter:");
      Console.ReadLine();

    }




    static string ByteArrayToString(byte[] ba)
    {
      StringBuilder hex = new StringBuilder(ba.Length * 2);
      foreach (byte b in ba)
        hex.AppendFormat("{0:X2}", b);
      return hex.ToString();
    }



    public static byte[] HexStrToByteArray(string hexStr)
    {
      byte tmp;

      byte[] b = new byte[hexStr.Length / 2];
      for (int i = 0; i < hexStr.Length; i += 2)
      {
        tmp = byte.Parse(hexStr.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
        b[i / 2] = tmp;
      }
      return b;
    }


  }
}
